package com.keda.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.List;

/*
* 前端每一个请求进入后端服务之前，都必须携带token,网关的作用就是进行token的检查，如果请求携带了token则放行，
* 否则则提示用户没有token,就无法访问
* */
//类前面加了@Component注解，表示该类被spring管理了
@Component
public class TokenCheckFilter implements GlobalFilter, Ordered {

    @Autowired //自动装配
    private StringRedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求的对象
        ServerHttpRequest request = exchange.getRequest();
        //获取请求头中的token
        List<String> authorization = request.getHeaders().get("Authorization");
        if(!CollectionUtils.isEmpty(authorization)){

        }
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
